rm(list=ls())

library(tricolore)
library(tidyverse)
library(gridExtra)
library(terra)
library(tidyterra)

try(setwd("___your directory here___") , silent=TRUE)

pres <- read.csv("data/pres.csv") |> 
  select(prov_code:island)
pres$kab_code <- as.character(pres$kab_code)

indo_kabs <- vect("data/gadm36_IDN_shp/gadm36_IDN_2.shp")
#to create comparison crosswalk
#write.csv(cbind(indo_kabs$NAME_1,indo_kabs$NAME_2,indo_kabs$CC_2), file="data/map_codes.csv")
recodes <- read.csv("data/map_recodes.csv")
recodes$kab_code_master[is.na(recodes$kab_code_master)] <- recodes$CC_2[is.na(recodes$kab_code_master)]
recodes <- recodes |> select(CC_2,kab_code_master)
recodes$CC_2 <- as.character(recodes$CC_2)
indo_kabs$CC_2 <- as.character(indo_kabs$CC_2)
indo_kabs <- left_join(indo_kabs, recodes, by="CC_2")
indo_kabs$CC_2 <- indo_kabs$kab_code_master

to.merge <- data.frame(kab_code=indo_kabs$CC_2)
to.merge$kab_code <- as.character(to.merge$kab_code)
  

to.ternary <- pres |>
  select(Prabowo, Anies, Ganjar, votes) |>
  mutate(p_prabowo = Prabowo/votes,
         p_anies = Anies/votes,
         p_ganjar = Ganjar/votes) |>
  select(p_prabowo, p_anies, p_ganjar)
tric <- Tricolore(to.ternary, p1="p_prabowo", p2="p_ganjar", p3="p_anies", breaks=3, 
                  hue=.7, chroma = .7, lightness=.33, contrast=.1, spread=1.5)
tric$key <- tric$key + xlab("") + ylab("") +
  labs(L = 'Prabowo', T = 'Ganjar', R = 'Anies') +
  theme(axis.title=element_text(size=6),
        axis.text=element_text(size=4))


tric$rgb <- recode(tric$rgb,
                   `#0050DB` = "#619cff",
                   `#A30000` = "#f7766d",
                   `#006600` = "#00bb38",
                   `#792574` = "#AC89B6",
                   `#005D5C` = "#31AC9C",
                   `#5A4800` = "#7C9953",
                   `#004B93` = "#6a9bc6",
                   `#852A2D` = "#b5887d",
                   `#005B00` = "#3AAb62"
                   )
pres$rgb <- tric$rgb

tric$key$data$rgb <- recode(tric$key$data$rgb,
                   `#0050DB` = "#619cff",
                   `#A30000` = "#f7766d",
                   `#006600` = "#00bb38",
                   `#792574` = "#AC89B6",
                   `#005D5C` = "#31AC9C",
                   `#5A4800` = "#7C9953",
                   `#004B93` = "#6a9bc6",
                   `#852A2D` = "#b5887d",
                   `#005B00` = "#3AAb62"
)

## hue .7
## chroma = .7
## lightness .3

#         blue                      green   bluered
#004B93   #0050DB   #005B00 #005D5C #006600 #792574 #852A2D 
#145      157       4     116       20      51       2 

#blue       #0050DB to #619cff
#red        #A30000 to #f7766d
#green      #006600 to #00bb38
#bluered    #792574 to #AC89B6
#bluegreen  #005D5C to #31AC9C
#redgreen   #594600 to #7C9953
#blueredbluegreen #004B93 to #6F9BA9
# red + bluered + redgreen #b5887d
#blueregredgreen #85292d to #949185 
# blue + bluered + bluegreen
#bluegreenredgreen #005B00 to #57A378
# green + bluegreen + regreen 

      
to.merge <- left_join(to.merge, pres, by="kab_code")
indo_kabs <- merge(indo_kabs, to.merge, by.x ="CC_2", by.y = "kab_code")

p <- ggplot(indo_kabs) +
  geom_spatvector(aes(fill = rgb), colour = "black", linewidth=.05) + 
  scale_fill_identity() + theme_void()
to.save <- grid.arrange(p, plot(tric$key), 
                        ncol=2, widths=c(2.5,1))

p <- p + theme(plot.margin=unit(c(-2,0,-4,0), "cm"))

java <- indo_kabs[indo_kabs$island %in% c("C./E. Java & Yogya","Jkt., W. Java, Banten"),]
p2 <- ggplot(java) +
  geom_spatvector(aes(fill = rgb), colour = "black", linewidth=.05) + 
  scale_fill_identity() + theme_void()
p2 <- p2 + theme(plot.margin=unit(c(-1,0,0,0), "cm"))
to.save2 <- grid.arrange(p2, plot(tric$key), 
                        ncol=2, widths=c(2.5,1))


final.figure <- grid.arrange(p,to.save2)
ggsave(filename = "output/figure2.png", plot = final.figure, width=6.5, height=5, units="in")
ggsave(filename = "output/figure2.pdf", plot = final.figure, width=6.5, height=5, units="in")
